package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import primetest.dto.DepartmentDto;
import primetest.impl.dao.DepartmentDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.DepartmentService;

public class DepartmentService implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -8060430686329121620L;
	private Transaction transaction;
	private DepartmentDao departmentDao;

	@Inject
	public DepartmentService(Transaction transaction,DepartmentDao officeDao){
		this.transaction = transaction;
		this.departmentDao = officeDao;
	}

	public List<DepartmentDto> getDepartmentList() throws IOException {
		try{
			transaction.begin();
			List<DepartmentDto> officeList = departmentDao.getAllList(transaction);
			//transaction.commit();
			return officeList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public DepartmentDto getDepartment(int departmentSeq) throws IOException {
		try{
			transaction.begin();
			DepartmentDto departmentDto = departmentDao.findDepartment(transaction, departmentSeq);
			//transaction.commit();
			return departmentDto;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void addDepartment(DepartmentDto officeDto) throws IOException {
		try{
			transaction.begin();
			departmentDao.addDepartment(transaction, officeDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updateDepartment(DepartmentDto officeDto) throws IOException {
		try{
			transaction.begin();
			departmentDao.updateDepartment(transaction, officeDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void deleteDepartment(int officeSeq) throws IOException {
		try{
			transaction.begin();
			departmentDao.deleteDepartment(transaction, officeSeq);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}
}
